package cn.edu.zut.business;

import cn.edu.zut.DAO.StudentDAO;
import cn.edu.zut.model.Student;
import cn.edu.zut.model.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CreditService {
    StudentDAO studentDAO ;

    @Autowired
    @Qualifier(value = "bdi1")
    public void setStudentDAO(StudentDAO studentDAO) {
        this.studentDAO = studentDAO;
    }

    public void sumCredit(double x, double y){
        List<Student> students = studentDAO.queryAllStudent();
        List<Course> courses;
        System.out.println(students);
        for(Student student:students){
            courses = student.getCourses();
            for(Course course:courses){
                course.setSumCredit((course.getTestCredit()*x+course.getRegularCredit()*y));
                System.out.println(course.getSumCredit());
            }
            studentDAO.modifySumCredit(student);
        }
    }

    /*
     for(int i=0;i<studens.size();i++) {
        List<Course> courses = studentDAO.queryAllCourse(studens.get(i));
        for(int j=0;j<studens.get(i).getCourses().size();j++) {
            Course course = studens.get(i).getCourses().get(j);
            course.setSumCredit((course.getTestCredit()*x+course.getRegularCredit()*y));
            System.out.println(course.getSumCredit());
        }
        studentDAO.modifySumCredit(studens.get(i));


         for(int i=0;i<students.size();i++) {
            List<Course> courses = studentDAO.queryOneStudent(students.get(i));
            for(int j=0;j<courses.size();j++) {
                Course course = courses.get(j);
                course.setSumCredit((course.getTestCredit()*x+course.getRegularCredit()*y));
                System.out.println(course.getSumCredit());
            }
            studentDAO.modifySumCredit(students.get(i));
        }
    }*/


    public  double savgCredit(Student student){
        double AllCourseCredit = 0;
        for(Course course : student.getCourses()){
            AllCourseCredit = AllCourseCredit + course.getSumCredit();
        }
        double avgCredit = AllCourseCredit/(student.getCourses().size());
        return avgCredit;
    }

    public void insertStudent(Student student){
        studentDAO.insertStudent(student);
    }

}
